package com.baida.retail.tasksystem.message.logic;

import com.alibaba.fastjson.JSON;
import com.baida.retail.tasksystem.enums.ErrorCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

@ApiModel("固定返回格式")
public class Result {
    @ApiModelProperty(value = "codeId")
    private int code;
    @ApiModelProperty(value = "msg")
    private String msg;
    @ApiModelProperty(value = "data")
    private Object data;

    public Result(ErrorCode error, String msg, Object data) {
        init(error, msg, data);
    }

    public Result(ErrorCode error, Object data) {
        init(error, error.getMsg(), data);
    }

    public Result(ErrorCode error, String msg) {
        init(error, msg, null);
    }

    public Result(ErrorCode error) {
        init(error, error.getMsg(), null);
    }

    public Result() {
        init(ErrorCode.SUCCESS, null, null);
    }

    private void init(ErrorCode error, String msg, Object data) {
        this.code = error.getId();
        this.msg = msg != null ? msg : error.getMsg();
        this.data = data;
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
}
